// 多态实现通用函数调用类
#ifndef __CALLER_HPP__
#define __CALLER_HPP__

#include <memory>
#include <iostream>

// lambda表达式/普通函数
class FunctionCaller
{
    struct Caller_base
    {
        virtual void call() = 0;
        virtual ~Caller_base() = default;
    };

    template <typename FUNCTION>
    struct Caller : public Caller_base
    {
        FUNCTION _function;

        Caller(const FUNCTION& function) : _function(function) {}

        virtual void call() override
        {
            _function();
        }
    };

private:
    std::unique_ptr<Caller_base> _caller; // _caller->call() --> 多态

public:
    template <typename FUNCTION>
    explicit FunctionCaller(FUNCTION function) // caller类代理
        : _caller(new Caller<FUNCTION>(function))
    {
        std::cout << "FunctionCaller -> function param type : " << typeid(function).name() << std::endl;
    }

    void operator()()
    {
        _caller->call();
    }

    FunctionCaller(const FunctionCaller &functionCaller) = delete;
    FunctionCaller &operator=(const FunctionCaller &functionCaller) = delete;

    // 移动构造/移动赋值 -> 满足临时对象构造/临死对象函数内返回
    FunctionCaller(FunctionCaller &&functionCaller) noexcept
        : _caller(std::move(functionCaller._caller)) {}

    FunctionCaller &operator=(FunctionCaller &&functionCaller) noexcept
    {
        if (this != &functionCaller)
        {
            _caller = std::move(functionCaller._caller);
        }
        return *this;
    }
};   
                 // class FunctionCaller
#endif  // #ifndef __CALLER_HPP__
